package com.java.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.FoodInfo;
import com.java.entity.FoodType;
import com.java.entity.event.FoodInfoEvent;
import com.java.entity.event.FoodTypeEvent;
import com.java.mapper.FoodInfoMapper;
import com.java.mapper.FoodTypeMapper;
import com.java.service.FoodService;
import com.java.util.MyMap;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class FoodServiceImpl implements FoodService {

	@Autowired private FoodInfoMapper foodMapper;

	@Autowired private FoodTypeMapper typeMapper;
	
	@Override
	public BaseRespone saveType(String typeId, String typeTitle, String typeIcon) {
		int row = 0;
		FoodType info = typeMapper.selectByKey(typeId);
		if(null == info) {
			info = new FoodType()
			.setTypeId(MyTools.getUUID())
			.setTypeTitle(typeTitle)
			.setTypeIcon(typeIcon)
			.setTypeSore(0)
			.setTypeTime(new Date());
			row = typeMapper.insertSelective(info);
		}else {
			info.setTypeTitle(typeTitle)
			.setTypeIcon(typeIcon);
			row = typeMapper.updateSelective(info);
		}
		return BaseRespone.result(row, "保存成功");
	}

	@Override
	public BaseRespone delType(String typeId) {
		int row = typeMapper.deleteByKey(typeId);
		return BaseRespone.result(row, "删除成功");
	}

	@Override
	public TableRespone getTypes(String typeTitle, int page, int size) {
		MyMap mm = new MyMap().put("typeTitle", typeTitle);
		List<FoodTypeEvent> list = typeMapper.selectAll(mm, page, size);
		long ll = typeMapper.selectAllCount(mm);
		return new TableRespone(list, ll);
	}

	@Override
	public BaseRespone saveFood(FoodInfo food) {
		int row = 0;
		FoodInfo info = foodMapper.selectByKey(food.getFoodId());
		if(null == info) {
			food.setFoodId(MyTools.getUUID())
			.setFoodTime(new Date());
			row = foodMapper.insertSelective(food);
		}else {
			row = foodMapper.updateSelective(food);
		}
		return BaseRespone.result(row, "保存成功");
	}

	@Override
	public BaseRespone delFood(String foodId) {
		int row = foodMapper.deleteByKey(foodId);
		return BaseRespone.result(row, "删除成功");
	}

	@Override
	public TableRespone getFoods(String foodTypeId, String foodTitle, int page, int size) {
		MyMap mm = new MyMap().put("foodTypeId", foodTypeId).put("foodTitle", foodTitle);
		List<FoodInfoEvent> list = foodMapper.selectAll(mm, page, size);
		for (FoodInfoEvent foodInfoEvent : list) {
			foodInfoEvent.setType(typeMapper.selectByKey(foodInfoEvent.getFoodTypeId()));
		}
		long ll = foodMapper.selectAllCount(mm);
		return new TableRespone(list, ll);
	}

}
